package com.liangzhong.proxy.leagueMatch
{
	import com.adobe.serialization.json.JSON;
	import com.liangzhong.commandconst.pub.CMD_Pub_Match;
	import com.liangzhong.constants.HttpRequestConst;
	import com.liangzhong.errortip.ErrorTips;
	import com.liangzhong.global.CMD_Head;
	import com.liangzhong.model.AppData;
	import com.liangzhong.proxy.BaseProxy;
	import com.liangzhong.proxy.ProxyUtils;
	import com.liangzhong.proxyconstant.leagueMatch.LeagueMatchConst;
	import com.socket.clientsocket.IClientSocket;
	
	import flash.net.URLVariables;
	import flash.utils.ByteArray;
	
	import lz.controls.Alert;

	/**
	 *联赛 
	 * @author lake
	 * 
	 */	
	public class LeagueMatchProxy extends BaseProxy
	{
		public static const ProxyName :String = "LeagueMatchProxy";
		
		public function LeagueMatchProxy(proxyName:String=ProxyName, data:Object=null)
		{
			super(proxyName, data);
		}
		/**
		 * 数据读取
		 * @param mainCmdID
		 * @param subCmdID
		 * @param buffer
		 * @param dataSize
		 * @param clientSocket
		 * @return 
		 */		
		override public function onSocketRead(mainCmdID:uint,
											  subCmdID:uint,
											  buffer:ByteArray, 
											  dataSize:int, 
											  clientSocket:IClientSocket):Boolean
		{
			switch(mainCmdID)
			{
				case CMD_Pub_Match.MAIN:
				{
					return onDeal(subCmdID,buffer,dataSize,clientSocket);
				}
			}
			return false;
		}
		/**
		 *处理信息 
		 * @param subCmdID
		 * @param buffer
		 * @param dataSize
		 * @param clientSocket
		 * @return 
		 * 
		 */		
		private function onDeal(subCmdID:uint, buffer:ByteArray, dataSize:int, clientSocket:IClientSocket):Boolean
		{
			var resObj :Object;
			var retcode :int;
			switch(subCmdID)
			{
				case CMD_Pub_Match.PRE_MATCH://预选赛
				{
					retcode = buffer.readInt();
					if(retcode != 0)
					{
						Alert.showErrorTip(ErrorTips.instance.getCErrorTip(retcode) + "");
					}
					this.sendNotification(LeagueMatchConst.MATCH_RESULT,{retcode:retcode});
					return true;
				}
				case CMD_Pub_Match.BASE_MATCH://甲级联赛 
				{
					retcode = buffer.readInt();
					if(retcode != 0)
					{
						Alert.showErrorTip(ErrorTips.instance.getCErrorTip(retcode) + "");
					}
					this.sendNotification(LeagueMatchConst.MATCH_RESULT,{retcode:retcode});
					return true;
				}
				case CMD_Pub_Match.CHAMPION_MATCH://冠军联赛 
				{
					retcode = buffer.readInt();
					if(retcode != 0)
					{
						Alert.showErrorTip(ErrorTips.instance.getCErrorTip(retcode) + "");
					}
					this.sendNotification(LeagueMatchConst.MATCH_RESULT,{retcode:retcode});
					return true;
				}
				case CMD_Pub_Match.LEGEND_MATCH://传奇
				{
					retcode = buffer.readInt();
					if(retcode != 0)
					{
						Alert.showErrorTip(ErrorTips.instance.getCErrorTip(retcode) + "");
					}
					this.sendNotification(LeagueMatchConst.MATCH_RESULT,{retcode:retcode});
					return true;
				}
				case CMD_Pub_Match.SIGN_STAR://签约球星
				{
					retcode = buffer.readInt();
					if(retcode != 0)
					{
						Alert.showErrorTip(ErrorTips.instance.getCErrorTip(retcode) + "");
					}
					else
					{
						var npcid :int = buffer.readInt();
						var starid :int = buffer.readInt();
						AppData.getInstance().leagueMatchManager.baseMatchInfoVo.signSuccess(npcid,starid);
						Alert.showErrorTip("签约成功");
					}
					return true;
				}
			}
			return false;
		}
		/**
		 *开始预选赛 
		 * @param param
		 * 
		 */		
		public function preMatchStart(param:Object):void
		{
			var type :int = AppData.getInstance().leagueMatchManager.lineUpType;
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size + 5,CMD_Pub_Match.MAIN,CMD_Pub_Match.PRE_MATCH,0);
			sendBuf.writeByte(type);
			sendBuf.writeInt(param.cid);
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
		}
		/**
		 *开始甲级联赛 
		 * @param param
		 * 
		 */		
		public function baseMatchStart(param:Object):void
		{
			var type :int = AppData.getInstance().leagueMatchManager.lineUpType;
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size + 5,CMD_Pub_Match.MAIN,CMD_Pub_Match.BASE_MATCH,0);
			sendBuf.writeByte(type);
			sendBuf.writeInt(param.cid);
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
		}
		/**
		 *开始欧冠联赛 
		 * @param param
		 * 
		 */		
		public function championMatchStart(param:Object):void
		{
			var type :int = AppData.getInstance().leagueMatchManager.lineUpType;
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size + 1,CMD_Pub_Match.MAIN,CMD_Pub_Match.CHAMPION_MATCH,0);
			sendBuf.writeByte(type);
			//sendBuf.writeInt(param.cid);
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
		}
		/**
		 *开始传奇赛 
		 * @param param
		 * 
		 */		
		public function legendMatchStart(param:Object):void
		{
			var type :int = AppData.getInstance().leagueMatchManager.lineUpType;
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size + 5,CMD_Pub_Match.MAIN,CMD_Pub_Match.LEGEND_MATCH,0);
			sendBuf.writeByte(type);
			sendBuf.writeInt(param.cid);
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
		}
		/**
		 *签约球星 
		 * @param param
		 * 
		 */		
		public function signStar(param:Object):void
		{
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size + 8,CMD_Pub_Match.MAIN,CMD_Pub_Match.SIGN_STAR,0);
			sendBuf.writeInt(param.cid);
			sendBuf.writeInt(param.starid);
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
		}
		/**
		 *请求联赛信息 
		 * @param $obj
		 * 
		 */
		public function getLeagueInfo(obj:Object):void
		{
			var variables:URLVariables = ProxyUtils.instance.getUrlVariables();
			variables.method = HttpRequestConst.LEAGUEMATCH_GET_INFO;
			ProxyUtils.instance.webClient(getLeagueInfoRes,variables,false);
		}
		/**
		 *请求联赛 返回信息 
		 * @param $data
		 * 
		 */
		private function getLeagueInfoRes(data:Object):void
		{
			var str:String = String(data);
			var results:Object = JSON.decode(str);
			var obj:Object = results[0];
			if(obj.retcode == 0)
			{
				AppData.getInstance().leagueMatchManager.ntype = obj.league.ntype;
			}else
			{
				Alert.showErrorTip(ErrorTips.instance.getErrorTip(obj.retcode));
			}
		}
		/**
		 *请求预选赛信息 
		 * @param $obj
		 * 
		 */
		public function getLeaguePreInfo(obj:Object):void
		{
			var variables:URLVariables = ProxyUtils.instance.getUrlVariables();
			variables.method = HttpRequestConst.LEAGUEMATCH_GET_PRE_INFO;
			ProxyUtils.instance.webClient(getLeaguePreInfoRes,variables,false);
		}
		/**
		 *请求预选赛 返回信息 
		 * @param $data
		 * 
		 */
		private function getLeaguePreInfoRes(data:Object):void
		{
			var str:String = String(data);
			var results:Object = JSON.decode(str);
			var obj:Object = results[0];
			if(obj.retcode == 0)
			{
				AppData.getInstance().leagueMatchManager.parseTrialMatchInfo(obj.leagueTrial);
			}else
			{
				Alert.showErrorTip(ErrorTips.instance.getErrorTip(obj.retcode));
			}
		}
		/**
		 *请求基础联赛信息 
		 * @param $obj
		 * 
		 */
		public function getLeagueBaseInfo(obj:Object):void
		{
			var variables:URLVariables = ProxyUtils.instance.getUrlVariables();
			variables.method = HttpRequestConst.LEAGUEMATCH_GET_BASE_INFO;
			ProxyUtils.instance.webClient(getLeagueBaseInfoRes,variables,false);
		}
		/**
		 *请求基础联赛 返回信息 
		 * @param $data
		 * 
		 */
		private function getLeagueBaseInfoRes(data:Object):void
		{
			var str:String = String(data);
			var results:Object = JSON.decode(str);
			var obj:Object = results[0];
			if(obj.retcode == 0)
			{
				AppData.getInstance().leagueMatchManager.parseBaseMatchInfo(obj.leaguePremier);
			}else
			{
				Alert.showErrorTip(ErrorTips.instance.getErrorTip(obj.retcode));
			}
		}
		/**
		 *请求冠军联赛信息 
		 * @param $obj
		 * 
		 */
		public function getLeagueChampionInfo(obj:Object):void
		{
			var variables:URLVariables = ProxyUtils.instance.getUrlVariables();
			variables.method = HttpRequestConst.LEAGUEMATCH_GET_CHAMPION_INFO;
			ProxyUtils.instance.webClient(getLeagueChampionInfoRes,variables,false);
		}
		/**
		 *请求冠军联赛 返回信息 
		 * @param $data
		 * 
		 */
		private function getLeagueChampionInfoRes(data:Object):void
		{
			var str:String = String(data);
			var results:Object = JSON.decode(str);
			var obj:Object = results[0];
			if(obj.retcode == 0)
			{
				AppData.getInstance().leagueMatchManager.parseChampionMatchInfo(obj.leagueEurope);
			}else
			{
				Alert.showErrorTip(ErrorTips.instance.getErrorTip(obj.retcode));
			}
		}
		/**
		 *请求俱乐部关系表信息 (签约球星) 
		 * @param $obj
		 * 
		 */
		public function getClubrelation(obj:Object):void
		{
			var variables:URLVariables = ProxyUtils.instance.getUrlVariables();
			variables.cid = AppData.getInstance().user.uid;
			variables.npcids = AppData.getInstance().leagueMatchManager.getSelfBaseNpcids().toString();
			variables.method = HttpRequestConst.LEAGUEMATCH_GET_CLUB_RELATION;
			ProxyUtils.instance.webClient(getClubrelationRes,variables,false);
		}
		/**
		 *请求俱乐部关系表信息 (签约球星)  返回信息 
		 * @param $data
		 * 
		 */
		private function getClubrelationRes(data:Object):void
		{
			var str:String = String(data);
			var results:Object = JSON.decode(str);
			var obj:Object = results[0];
			if(obj.retcode == 0)
			{
				AppData.getInstance().leagueMatchManager.baseMatchInfoVo.setClubRelation(obj.clubrelation);
			}else
			{
				Alert.showErrorTip(ErrorTips.instance.getErrorTip(obj.retcode));
			}
		}
	}
}